package com.example.l.zcq1;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.ToggleButton;

import java.util.ArrayList;

public class shebeikongzhi extends Fragment {
    //声明一个表格对象
    private GridView gridView;
    //声明一个图片集合
    private ArrayList<ceshiModel> list=new ArrayList<>();
    //图片数组集合
    private int[] image={R.mipmap.baojingqi,R.mipmap.choushuibeng,R.mipmap.tongfengji,R.mipmap.zhaomingdeng};

    //声明一个自定义的适配器
    MyAdapter myAdapter;
    @Override
    public View onCreateView(LayoutInflater layoutInflater, ViewGroup viewGroup, Bundle b) {
        //获取图片显示主框架的对象
        View view = layoutInflater.inflate(R.layout.sbkz, viewGroup, false);
        //获取主框架中表格控件
        gridView=(GridView)view.findViewById(R.id.sbkz_gridview_1) ;
        //将数据放到集合当中
        list.clear();
        list.add(new ceshiModel("通风机：开启","true","运行时长：00时16分"));
        list.add(new ceshiModel("照明灯：关闭","false",""));
        list.add(new ceshiModel("抽水泵：开启","true","运行时长：00时33分"));
        list.add(new ceshiModel("报警器：开启","true","运行时长：00时50分"));

        myAdapter=new MyAdapter();
        //加载数据绑定
        gridView.setAdapter(myAdapter);

        return view;
    }

    public class MyAdapter extends BaseAdapter{

        @Override
        public int getCount(){return list.size();}

        @Override
        public Object getItem(int position){return null;}

        @Override
        public long getItemId(int position){return 0;}

        //实现模板页面的动态加载
        @Override
        public View getView(int position,View convertView,ViewGroup parent){
            //通过模板实例化控件
            View view=LayoutInflater.from(getActivity()).inflate(R.layout.sbkz_item_view,null);
            //通过模板对象获取模板中的控件
            TextView textView1=(TextView)view.findViewById(R.id.sbkz_textview_1);
            TextView textView2=(TextView)view.findViewById(R.id.sbkz_textview_2);

            ImageView imageView1=(ImageView)view.findViewById(R.id.sbkz_imageview_1);
            ToggleButton toggleButton1=(ToggleButton)view.findViewById(R.id.sbkz_bogglebutton_1);

            //从集合当中获取数据并加载到相对应的控件上
            String name=list.get(position).getImage_name();
            textView1.setText(name);
            textView2.setText(list.get(position).getImage_title());
            imageView1.setImageResource(image[position]);
            toggleButton1.setChecked(false);

            return view;
        }

    }
}
